/*
 * Copyright 2004-2005 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.base.web.service;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.orm.ObjectRetrievalFailureException;

import com.companyname.common.base.model.BaseObject;
import com.companyname.common.sysframe.define.SysFrameParams;
import com.companyname.common.sysframe.model.User;

/**
 * 作废然后新增model服务模板
 *
 */

public class BlankOutThenSaveModelTempletService extends TempletService
{
        static Logger logger = Logger.getLogger(BlankOutThenSaveModelTempletService.class);

        public void executeService(HttpServletRequest request)
        {
                BaseObject bo = (BaseObject)request.getAttribute(this.manager.getEntityClass().getName());
                this.iniUserInforIntoOldBO(request,bo);
                if(bo.getPri() == null)
                {
                        BaseObject oldBo = (BaseObject)this.manager.get(bo.getIdentity());
                        bo.setPri(oldBo.getPri());
                        bo.setCreator(oldBo.getCreator());
                        bo.setCreateOrg(oldBo.getCreateOrg());
                        bo.setCreateTime(oldBo.getCreateTime());
                }
                // 作废然后新增BaseObject
                logger.debug("1. 作废然后新增");
                this.manager.blankOutThenSave(bo);
                this.manager.update(bo);
        }

        /** 执行服务前（for 操作日志）*/
        public String getDetailBeforeExe(HttpServletRequest request)
        {
                BaseObject newBO = (BaseObject)request.getAttribute(this.manager.getEntityClass().getName());
                BaseObject oldBO;
                try
                {
                        oldBO = (BaseObject)this.manager.get(newBO.getIdentity());
                }
                catch(ObjectRetrievalFailureException orfe)
                {
                        oldBO = null;
                }
                catch(IllegalArgumentException iare)
                {
                        oldBO = null;
                }
                logger.info("\"作废\"然后新增服务");
                StringBuffer sb = new StringBuffer(1000);
                if(oldBO != null)
                {
                        sb.append("<old>").append(oldBO.getModelDetailXML()).append("</old>");

                }
                else
                {
                        sb.append("<old>").append("不存在").append("</old>");
                }
                return sb.toString();
        }

        /** 执行服务后（for 操作日志）*/
        public String getDetailAfterExe(HttpServletRequest request)
        {
                BaseObject newBO = (BaseObject)request.getAttribute(this.manager.getEntityClass().getName());
                logger.info("作废然后\"新增\"服务");
                StringBuffer sb = new StringBuffer(1000);
                sb.append("<new>").append(newBO.getModelDetailXML()).append("</new>");
                return sb.toString();
        }
}
